import json
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.patches import Ellipse
import numpy as np
from 翻译包 import *
from 文件工具箱 import *
from 句段处理 import *

数据文件 = "./分析结果.json"

def 画柱状图(测量指标, 纵坐标范围, 纵坐标标签):
	global 数据文件
	测量结果 = 恢复数据结构(数据文件) 
	sf = []
	fr = []
	hf = []
	hp = []
	el = []
	pr = []
	for 数据集 in ["caida", "tsinghua", "hgc", "telecom"]:
		sf.append(测量结果[数据集]["sf"][测量指标])
		fr.append(测量结果[数据集]["fr"][测量指标])
		hf.append(测量结果[数据集]["hf"][测量指标])
		hp.append(测量结果[数据集]["hp"][测量指标])
		el.append(测量结果[数据集]["el"][测量指标])
		pr.append(测量结果[数据集]["pr"][测量指标])

	for 数据集, _sf, _fr, _hf, _hp, _pr, _el in \
		zip(["CD", "TH", "GC", "CT"], sf, fr, hf, hp, pr, el):
		输出("数据集:%s, sf:%.4f, fr:%.4f, hf:%.4f, hp:%.4f, pr:%.4f, el:%.4f" \
			% (数据集, _sf, _fr, _hf, _hp, _pr, _el))
		输出("(_sf-_el)/_el:%.3f" % ((_sf-_el)/_el))
	fig = plt.figure(1)
	ax = fig.add_axes([0,0,1,1])
	ax.set_xlim([0.5,4.5])
	ax.set_ylim(纵坐标范围)
	plt.ylabel(纵坐标标签)
	plt.xticks(range(1, 5), ["CAIDA", "Campus", "ISP1", "ISP2"])
	rects = ax.patches
	plt.bar(np.arange(1, 5) - 0.375, sf, label = "SF", color = 'red', \
			width = 0.15, hatch = "o")
	plt.bar(np.arange(1, 5) - 0.225, fr, label = "FR", color = 'green', \
			width = 0.15, hatch = "/")
	plt.bar(np.arange(1, 5) - 0.075, hf, label = "HF", color = 'purple', \
			width = 0.15, hatch = "\\")
	plt.bar(np.arange(1, 5) + 0.075, hp, label = "HP", color = 'blue', \
			width = 0.15, hatch = "+")
	plt.bar(np.arange(1, 5) + 0.225, el, label = "EL", color = 'magenta', \
			width = 0.15, hatch = "-")
	plt.bar(np.arange(1, 5) + 0.375, pr, label = "PR", color = 'yellow', \
			width = 0.15, hatch = "x")
	plt.legend(bbox_to_anchor=(0.0, 1.0, 1.0, 0.1), loc = 3, ncol = 3, mode = "expand", borderaxespad = 0.0)
#	plt.legend(ncol = 3)
	plt.xlabel("Traces")
	plt.ylabel(纵坐标标签)
	plt.savefig("%s.pdf" % 测量指标, bbox_inches = "tight")
	plt.close()
